﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RibbonEditor.Forms.WebRessources
{
    public partial class WebResourcePicker : Form
    {
        XmlNode resourcesNode;

        public string SelectedResource { get; set; }

        public WebResourcePicker(XmlNode webResourcesNode)
        {
            InitializeComponent();

            this.resourcesNode = webResourcesNode;
            this.FillList();
        }

        private void FillList()
        {
            this.lstWebResources.Items.Clear();

            if (this.resourcesNode != null)
            {
                foreach (XmlNode webResourceNode in this.resourcesNode.SelectNodes("WebResource"))
                {
                    string sType = Enum.GetName(typeof(Enumerations.WebResourceType), int.Parse(webResourceNode.SelectSingleNode("WebResourceType").InnerText));

                    XmlNode displayNameXmlNode = webResourceNode.SelectSingleNode("DisplayName");

                    ListViewItem item = new ListViewItem(displayNameXmlNode != null ? displayNameXmlNode.InnerText : webResourceNode.SelectSingleNode("Name").InnerText);
                    item.SubItems.Add(webResourceNode.SelectSingleNode("Name").InnerText);
                    item.SubItems.Add(sType);
                    item.SubItems.Add(webResourceNode.SelectSingleNode("FileName").InnerText);
                    item.Tag = webResourceNode;

                    this.lstWebResources.Items.Add(item);
                }
            }
        }

        private void btnNewResource_Click(object sender, EventArgs e)
        {
            CreateWebResourceDialog cwrDialog = new CreateWebResourceDialog(this.resourcesNode);
            cwrDialog.StartPosition = FormStartPosition.CenterParent;

            if (cwrDialog.ShowDialog() == DialogResult.OK)
            {
                FillList();
            }
        }

        private void lstWebResources_DoubleClick(object sender, EventArgs e)
        {
            if (this.lstWebResources.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstWebResources.SelectedItems[0].Tag;
                this.SelectedResource = node.SelectSingleNode("Name").InnerText;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            if (this.lstWebResources.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstWebResources.SelectedItems[0].Tag;
                CreateWebResourceDialog cwrDialog = new CreateWebResourceDialog(node);
                cwrDialog.StartPosition = FormStartPosition.CenterParent;

                cwrDialog.ShowDialog();
            }
        }

        private void btnWebResourcePickerValidate_Click(object sender, EventArgs e)
        {
            if (this.lstWebResources.SelectedItems.Count > 0)
            {
                this.SelectedResource = this.lstWebResources.SelectedItems[0].SubItems[1].Text;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Please select a web resource!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnWebResourcePickerCancel_Click(object sender, EventArgs e)
        {
            this.SelectedResource = null;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
